import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.concurrent.*;

/**
 * @author 王艺锡
 * @version 1.0
 */
@Slf4j
public class test1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {


    }

    private static void method2() {
        //如何让每周六下午六点执行某个任务
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //获取周六时间
        LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.SATURDAY);
        //如果当前时间是在周六之后
        if(now.compareTo(time) > 0){
            //在time的基础上再加上一周
            time = time.plusWeeks(1);
        }

        //使用ScheduledExecutorService线程池设置开始时间和每个多长时间执行一次
        //initialDelay 从当前时间开始延迟多长时间后执行任务
        //period 每隔多久执行一次
        long initialDelay = LocalDateTimeUtil.between(now,time).toMillis();
        long period = 1000 * 60 * 60 * 24 * 7;
        pool.scheduleAtFixedRate(() ->{
            log.debug("执行任务逻辑");
        },initialDelay,period,TimeUnit.MILLISECONDS);
    }


    private static void method1(ExecutorService pool) throws InterruptedException, ExecutionException {
        //submit方法可以返回线程中执行的结果，execute方法不可以
        Future<String> future = pool.submit(() -> {
            //执行业务逻辑
            log.debug("执行业务逻辑");
            Thread.sleep(1000);
            return "ok";
        });

        //主线程中获取返回的结果
        log.debug("{}",future.get());
    }
}
